import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus';
import * as common from '../../../utils/common'
import * as compDiviService from '../../../services/AccountDivi';
const daoName = "VBaseKhjlDjdfDhls";
const daoNameImp = "BaseKhjlDjdfDhls";
const ButtonGroup = Button.Group;
const columns = [
    { title: '工号', width: 100, key: 'CUID', dataIndex: "CUID", sorter: true, dispaly: true, filter: true, export: false },
    { title: '姓名', width: 120, sorter: true, dataIndex: 'CUNM', key: 'CUNM', dispaly: true, filter: false, export: true },
    { title: '放款操作(10.00分)', width: 180, sorter: true, dataIndex: 'FKFZ', key: 'FKFZ', dispaly: true, filter: false, export: true },
    { title: '授信后管理(10.00分)', width: 180, sorter: true, dataIndex: 'SXFZ', key: 'SXFZ', dispaly: true, filter: false, export: true },
    { title: '年份', width: 130, sorter: true, key: 'NIAN', dataIndex: 'NIAN', dispaly: true, filter: false, export: true },
    { title: '季度', width: 130, sorter: true, key: 'JIDU', dataIndex: 'JIDU', dispaly: true, filter: false, export: true },
];
const controls = [
    { name: 'CUID', required: true, type: 'text', label: '工号', grid: 10 / 24 },
    { name: 'CUNM', required: true, type: 'text', label: '姓名', grid: 10 / 24 },
    { name: 'FKFZ', required: false, type: 'number', dataType: "number", label: '放款操作', grid: 10 / 24 },
    { name: 'SXFZ', required: false, type: 'number', dataType: "number", label: '授信后管理', grid: 10 / 24 },
    { name: 'NIAN', required: true, type: 'text', label: '年份', grid: 10 / 24 },
    { name: 'JIDU', required: true, type: 'text', label: '季度', grid: 10 / 24 },
]
const columnsImp = [
    { title: '工号', key: 'CUID', width: 100, export: true },
    { title: '姓名', key: 'CUNM', width: 100, export: true },
    { title: '序列', key: 'XL', width: 100, export: true },
    { title: '职务', key: 'ZW', width: 100, export: true },
    { title: '部门', key: 'BM', width: 100, export: true },
    { title: '放款操作(10.00分)', key: 'FKFZ', width: 300, export: true },
    { title: '授信后管理(10.00分)', key: 'SXFZ', width: 300, export: true },
];
const controlsImp = [
    { name: 'CUID', required: true, type: 'text', label: '工号', grid: 10 / 24 },
    { name: 'CUNM', required: true, type: 'text', label: '姓名', grid: 10 / 24 },
    { name: 'FKFZ', required: false, type: 'number', dataType: "number", label: '放款操作', grid: 10 / 24 },
    { name: 'SXFZ', required: false, type: 'number', dataType: "number", label: '授信后管理', grid: 10 / 24 },
    { name: 'NIAN', required: true, type: 'text', label: '年份', grid: 10 / 24 },
    {
        name: 'JIDU', required: true, type: 'select', label: '季度', grid: 10 / 24,
        data: [{ value: '1', label: "第一季度" }, { value: '2', label: "第二季度" }, { value: '3', label: "第三季度" }, { value: '4', label: "第四季度" }]
    },
]
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoNameImp);
class BaseKhjlDjdfDhlsForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });
        let dates = new Date()
        let month= dates.getMonth()+1
        let JIDU ='1'
        if(month<=3){
            JIDU='1'
        }else if(month<=6){
            JIDU='2'
        }else if(month<=9){
            JIDU='3'
        }else{
            JIDU='4'
        }
        let whereItems = [];
        let WhereCauses = [];
        whereItems.push({
            FieldName: "NIAN",
            Operator: "=",
            FieldValues: dates.getFullYear()+''
        })
        whereItems.push({
            FieldName: "JIDU",
            Operator: "=",
            FieldValues: JIDU
        })
        WhereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        let seeks=WhereCauses
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 10,seeks }
        });
        this.loadOptionData();
    }
    componentWillReceiveProps(nextProps) {
        const { showImport } = this.props
        if (!nextProps.showImport && showImport) {
            this.props.dispatch({
                type: `${daoName}/fetch`,
                payload: { daoName, page: 1, pageSize: 10 }
            });
        }
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    showImportHandler() {
        compDiviService.SelConfUpdt().then(function (data) {
            if (data==='false') {
                message.info("数据补录开关未开启")
            } else {
                this.props.dispatch({
                    type: `${daoNameImp}_DataImport/setDisplay`,
                    payload: { display: true },
                });
            }
        }.bind(this))
    }
    DownTmportHandler() {
        let columnHeaders = []
        columnsImp.map((c, j) => {
            if (!c.export) {
                return
            }
            let header = {
                Name: c.key,
                Caption: c.title,
                Width: c.width
            }
            columnHeaders.push(header)
        })
        let form = document.createElement('form');//定义一个form表单        
        const whereCauses = []
        let sort = {
            field: 'BM',
            order: 'descend'
        }
        const orderItems = common.sorterToOrderItems(sort)
        form.style = "display:none";
        form.target = "_blank";
        form.method = "post";
        form.action = "/api/ExportXlsx/" + "VBaseKhjlDjdfDhlsEx";
        let input1 = document.createElement("input");
        input1.type = "hidden";
        input1.name = "WhereCauses";
        input1.value = JSON.stringify(whereCauses);
        let input2 = document.createElement("input");
        input2.type = "hidden";
        input2.name = "OrderItems";
        input2.value = JSON.stringify(orderItems)
        let input3 = document.createElement("input");
        input3.type = "hidden";
        input3.name = "ColumnHeaders";
        input3.value = JSON.stringify(columnHeaders);
        let input4 = document.createElement("input");
        input4.type = "hidden";
        input4.name = "PermID";
        input4.value = "" + this.props.permID;
        let input5 = document.createElement("input");
        input5.type = "hidden";
        input5.name = "UserID";
        input5.value = "" + this.props.userID;
        document.body.appendChild(form);//将表单放置在web中
        form.appendChild(input1);
        form.appendChild(input2);
        form.appendChild(input3);
        form.appendChild(input4);
        form.appendChild(input5);
        form.submit();
        document.body.removeChild(form)
    }
    findData(){
        let NIAN='2018'
        let JIDU='1'
        let isOK=true
        const form = this.refs.editForm.getForm();
        form.validateFields((err, values) => {
            if (!err) {
                if (values.NIAN === undefined || values.NIAN === '') {
                    message.info('请输入年份！');
                    isOK = false;
                } else {
                    NIAN = values.NIAN;
                }
                if (values.JIDU === undefined || values.JIDU === '') {
                    message.info('请选择季度！');
                    isOK = false;
                } else {
                    JIDU = values.JIDU;
                }
            }
        });
        let whereItems = [];
        let WhereCauses = [];
        whereItems.push({
            FieldName: "NIAN",
            Operator: "=",
            FieldValues: NIAN
        })
        whereItems.push({
            FieldName: "JIDU",
            Operator: "=",
            FieldValues: JIDU
        })
        WhereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        if(!isOK){
            return
        }
        let seeks=WhereCauses
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 10,seeks }
        });
    }
    render() {
        let { showEdit, record, showImport, permID, popedomTypes, pOrgID } = this.props
        const pTypes = popedomTypes[permID]
        let datas = []
        const controlsFilter = [];
        if (pTypes.indexOf("2") !== -1) {
            datas.push({ value: '0', label: '查询', onClick: this.findData.bind(this) }, { value: '1', label: '导入', onClick: this.showImportHandler.bind(this) }, { value: '2', label: '下载导入模板', onClick: this.DownTmportHandler.bind(this) })
            controlsFilter.push(
                { name: 'NIAN', required: false, type: 'text', label: '年份', grid: 4 / 24, layout: { labelSpan: 12, wrapperSpan: 12 } },
                {
                    name: 'JIDU', required: false, type: 'select', label: '季度', grid: 4 / 24, layout: { labelSpan: 10, wrapperSpan: 14 },
                    data: [{ value: '1', label: "第一季度" }, { value: '2', label: "第二季度" }, { value: '3', label: "第三季度" }, { value: '4', label: "第四季度" }]
                }
            )
            controlsFilter.push({ name: 'CX', required: false, type: 'button-group', data: datas, grid: 10 / 24 })
        }
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        <EditFrom ref="editForm" daoName={daoName} controls={controlsFilter} record={record} clearItemMargin={true} />
                    </div>
                    <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
                    <DataImport title="综合素质考评-贷后风险管控" importName="BaseKhjlDjdfDhls" daoName={daoNameImp} keys={["DHID"]} paramNames={["NIAN","JIDU"]} importMode="TruncteAfterInsert" controls={controlsImp} />
                </div>
            </div>
        )
    }
}

function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes } = state.menus;
    const { display } = state[`${daoNameImp}_DataImport`];
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record,
        showImport: display
    }
}
BaseKhjlDjdfDhlsForm = connect(mapStateToProps)(BaseKhjlDjdfDhlsForm)
export default BaseKhjlDjdfDhlsForm;

let render = (props) => React.createElement(BaseKhjlDjdfDhlsForm, props);
let f = new Factory()
f.set("BaseKhjlDjdfDhlsForm", render)
